<?php 
  require_once "./spyc.php";
  require_once "./HyperDown/Parser.php";


  $DEFAULT_ROOT = "./";
  $DEFAULT_FILE = "README.md";

  $config = Spyc::YAMLLoad('./conf.yml');
  $file_root = $config["root"];
  $file_root = (isset($file_root) && is_dir(trim($file_root))) ? trim($file_root) : $DEFAULT_ROOT;

  preg_match("/^[^?]*/",$_SERVER["REQUEST_URI"],$out);
  $file = $file_root.$out[0];

  if(preg_match("/(\\/|\\\)$/i",$file) && is_file($file.$DEFAULT_FILE)){
    $file = $file.$DEFAULT_FILE;
  }
  
  if(!is_file($file)){
    echo "文件未找到！";
    exit(1); 
  }

  
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    html,body{
      margin: 0;
      padding: 0;
    }

    body{
      padding: 10px 50px 100px 50px;
    }
    #root {

    }
  </style>
</head>
<body>
  <div id="root">
<?php

  $parser = new HyperDown\Parser;
  $md =  fopen($file,'r');

  $html = $parser -> makeHtml($md);
  while(!feof($md)){
   echo $parser->makeHtml(fgets($md));
  }
  fclose($md);


?>
  </div>
</body>
</html>

